package great.easychat.help;

import android.app.Application;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.util.Log;

import com.pince.http.HttpHelper;
import com.pince.renovace2.RenovaceCode;
import com.pince.ut.AppCache;
import com.pince.ut.AppUtil;
import com.pince.ut.constans.Constants;
import com.pince.ut.helper.ActivityManager;
import com.tencent.bugly.Bugly;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.umeng.analytics.MobclickAgent;
import com.umeng.commonsdk.UMConfigure;
import com.umeng.socialize.PlatformConfig;

import java.util.List;

import cn.net.shoot.sharetracesdk.ShareTrace;
import great.easychat.help.config.AppHttpConfig;
import great.easychat.help.update.FileConstants;
import great.easychat.help.util.ToastUtil;
import io.reactivex.plugins.RxJavaPlugins;

import static great.easychat.help.Constant.BUGLY_ID;
import static great.easychat.help.Constant.UMENG_CHANNEL;
import static great.easychat.help.Constant.UMENG_KEY;

/**
 * Created by Administrator on 2018/08/13 0013.
 */

public class CustomAppcation extends Application {

    public static Context context;
    private static final String TAG = "Tinker.CustomAppcation";

    public CustomAppcation() {

    }

    @Override
    public void attachBaseContext(Context base) {
        super.attachBaseContext(base);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        context = this;
        ShareTrace.init(this);
        ToastUtil.init(getApplicationContext());
        Log.d("CustomAppcation", "CustomAppcation");
        AppCache.setContext(this);
        UMConfigure.preInit(context, UMENG_KEY, UMENG_CHANNEL);
        MobclickAgent.setScenarioType(context, MobclickAgent.EScenarioType.E_UM_NORMAL);
        MobclickAgent.setCatchUncaughtExceptions(false);
        //Activity栈管理类
        ActivityManager.get().init(this);
        Bugly.init(context, BUGLY_ID, BuildConfig.DEBUG);
        initShare();
        FileConstants.initFileConfig(this);
        api = WXAPIFactory.createWXAPI(context, "wxb28e0f05a97986ed", true);
        api.registerApp("wxb28e0f05a97986ed");
        if (AppUtil.isMainProcess(context)) {
            HttpHelper.init(context, "https://www.baidu.com/", AppHttpConfig.class, BuildConfig.DEBUG);
        }
    }

    /**
     * 我们需要确保至少对主进程跟patch进程初始化 TinkerPatch
     */

    private void initShare() {
        PlatformConfig.setWeixin("wxb28e0f05a97986ed", "e6df36b22f6813f06cf787c4ab0efe5a");
    }

    static IWXAPI api;

    /**
     * 判断微信客户端是否存在
     *
     * @return true安装, false未安装
     */
    public static boolean isWeChatAppInstalled() {
        if (api.isWXAppInstalled()) {
            return true;
        } else {
            final PackageManager packageManager = context.getPackageManager();// 获取packagemanager
            List<PackageInfo> pinfo = packageManager.getInstalledPackages(0);// 获取所有已安装程序的包信息
            if (pinfo != null) {
                for (int i = 0; i < pinfo.size(); i++) {
                    String pn = pinfo.get(i).packageName;
                    if (pn.equalsIgnoreCase("com.tencent.mm")) {
                        return true;
                    }
                }
            }
            return false;
        }
    }

}
